package dao.hibernate;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import model.CauHoiTrucTuyen;
import model.ChuDe;
import model.NguoiDung;
import dao.CauHoiTrucTuyenDAO;

public class CauHoiTrucTuyenDAOHibernate extends
		GenericHibernateDAO<CauHoiTrucTuyen, Long> implements
		CauHoiTrucTuyenDAO {
	@SuppressWarnings("unchecked")
	@Override
	public List<CauHoiTrucTuyen> findByIdNguoiHoi(String userId) {
		List<CauHoiTrucTuyen> list = new ArrayList<CauHoiTrucTuyen>();
		Query query = getSession().createQuery("SELECT cauHoi.idCauHoiTrucTuyen, cauHoi.chuDe, cauHoi.tieuDe, cauHoi.nguoiHoi, cauHoi.ngayHoi, cauHoi.traLoi, cauHoi.daXem FROM CauHoiTrucTuyen AS cauHoi WHERE cauHoi.nguoiHoi.idNguoiDung = :pname ORDER BY cauHoi.ngayHoi")
		.setString("pname", userId);
		Iterator iter = query.list().iterator();
		CauHoiTrucTuyen cauHoi = null;
		while(iter.hasNext()){
			Object[] row = (Object[])iter.next();
			long idCauHoi = (Long)row[0];
			ChuDe chuDe = (ChuDe)row[1];
			String tieuDe = (String)row[2];
			NguoiDung nguoiHoi = (NguoiDung)row[3];
			Date ngayHoi = (Date)row[4];
			String traLoi = (String)row[5];
			boolean daXem = (Boolean)row[6];
			NguoiDung nguoiHoiSimple = new NguoiDung(nguoiHoi.getIdNguoiDung(), null, null, nguoiHoi.getHoTen(), nguoiHoi.getEmail());
			cauHoi = new CauHoiTrucTuyen(chuDe, tieuDe, null, nguoiHoiSimple, ngayHoi, traLoi, null, null);
			cauHoi.setIdCauHoiTrucTuyen(idCauHoi);
			cauHoi.setDaXem(daXem);
			list.add(cauHoi);
		}
		return list;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<CauHoiTrucTuyen> findByIdNguoiHoiLike(String userId) {
		return getSession()
				.createQuery(
						"FROM CauHoiTrucTuyen AS cauHoi WHERE cauHoi.nguoiHoi.idNguoiDung LIKE :pname")
				.setString("pname", "%" + userId + "%").list();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<CauHoiTrucTuyen> findUnreadByIdNguoiHoi(String userId) {
		List<CauHoiTrucTuyen> list = new ArrayList<CauHoiTrucTuyen>();
		Query query = getSession().createQuery("SELECT cauHoi.idCauHoiTrucTuyen, cauHoi.chuDe, cauHoi.tieuDe, cauHoi.nguoiHoi, cauHoi.ngayHoi, cauHoi.traLoi, cauHoi.daXem FROM CauHoiTrucTuyen AS cauHoi WHERE cauHoi.nguoiHoi.idNguoiDung = :pname AND cauHoi.traLoi != null AND cauHoi.daXem = false ORDER BY cauHoi.ngayHoi")
		.setString("pname", userId);
		Iterator iter = query.list().iterator();
		CauHoiTrucTuyen cauHoi = null;
		while(iter.hasNext()){
			Object[] row = (Object[])iter.next();
			long idCauHoi = (Long)row[0];
			ChuDe chuDe = (ChuDe)row[1];
			String tieuDe = (String)row[2];
			NguoiDung nguoiHoi = (NguoiDung)row[3];
			Date ngayHoi = (Date)row[4];
			String traLoi = (String)row[5];
			boolean daXem = (Boolean)row[6];
			NguoiDung nguoiHoiSimple = new NguoiDung(nguoiHoi.getIdNguoiDung(), null, null, nguoiHoi.getHoTen(), nguoiHoi.getEmail());
			cauHoi = new CauHoiTrucTuyen(chuDe, tieuDe, null, nguoiHoiSimple, ngayHoi, traLoi, null, null);
			cauHoi.setIdCauHoiTrucTuyen(idCauHoi);
			cauHoi.setDaXem(daXem);
			list.add(cauHoi);
		}
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CauHoiTrucTuyen> findByIdNguoiTraLoi(String userId) {
		List<CauHoiTrucTuyen> list = new ArrayList<CauHoiTrucTuyen>();
		Query query = getSession().createQuery("SELECT cauHoi.idCauHoiTrucTuyen, cauHoi.chuDe, cauHoi.tieuDe, cauHoi.nguoiHoi, cauHoi.ngayHoi, cauHoi.traLoi, cauHoi.daXem FROM CauHoiTrucTuyen AS cauHoi WHERE cauHoi.nguoiTraLoi.idNguoiDung = :pname ORDER BY cauHoi.ngayHoi")
		.setString("pname", userId);
		Iterator iter = query.list().iterator();
		CauHoiTrucTuyen cauHoi = null;
		while(iter.hasNext()){
			Object[] row = (Object[])iter.next();
			long idCauHoi = (Long)row[0];
			ChuDe chuDe = (ChuDe)row[1];
			String tieuDe = (String)row[2];
			NguoiDung nguoiHoi = (NguoiDung)row[3];
			Date ngayHoi = (Date)row[4];
			String traLoi = (String)row[5];
			boolean daXem = (Boolean)row[6];
			NguoiDung nguoiHoiSimple = new NguoiDung(nguoiHoi.getIdNguoiDung(), null, null, nguoiHoi.getHoTen(), nguoiHoi.getEmail());
			cauHoi = new CauHoiTrucTuyen(chuDe, tieuDe, null, nguoiHoiSimple, ngayHoi, traLoi, null, null);
			cauHoi.setIdCauHoiTrucTuyen(idCauHoi);
			cauHoi.setDaXem(daXem);
			list.add(cauHoi);
		}
		return list;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<CauHoiTrucTuyen> findByIdNguoiTraLoiLike(String userId) {
		return getSession()
				.createQuery(
						"FROM CauHoiTrucTuyen AS cauHoi WHERE cauHoi.nguoiTraLoi.idNguoiDung LIKE :pname")
				.setString("pname", "%" + userId + "%").list();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<CauHoiTrucTuyen> findByTenChuDe(String name) {
		return getSession()
		.createQuery(
				"SELECT cauHoi FROM CauHoiTrucTuyen AS cauHoi JOIN cauHoi.chuDe AS chuDe WHERE chuDe.tenChuDe LIKE :pname")
		.setString("pname", "%" + name + "%").list();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<CauHoiTrucTuyen> findByTieuDe(String name) {
		return getSession()
		.createQuery(
				"FROM CauHoiTrucTuyen AS cauHoi WHERE cauHoi.tieuDe LIKE :pname")
		.setString("pname", "%" + name + "%").list();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<CauHoiTrucTuyen> findByNgayHoi(String date) {
		return getSession()
		.createQuery(
				"FROM CauHoiTrucTuyen AS cauHoi WHERE cauHoi.ngayHoi LIKE :pname")
		.setString("pname", "%" + date + "%").list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CauHoiTrucTuyen> findByNgayTraLoi(String date) {
		return getSession()
		.createQuery(
				"FROM CauHoiTrucTuyen AS cauHoi WHERE cauHoi.ngayTraLoi LIKE :pname")
		.setString("pname", "%" + date + "%").list();
	}

	public List<CauHoiTrucTuyen> findByOneCondition(String criteria,
			String keyWord) {
		List<CauHoiTrucTuyen> result = new ArrayList<CauHoiTrucTuyen>();
		if (criteria.equals("tenChuDe")) {
			result = findByTenChuDe(keyWord);
		} else if (criteria.equals("tieuDe")){
			result = findByTieuDe(keyWord);
		} else if (criteria.equals("idNguoiHoi")) {
			result = findByIdNguoiHoiLike(keyWord);
		} else if (criteria.equals("idNguoiTraLoi")) {
			result = findByIdNguoiTraLoiLike(keyWord);
		} else if (criteria.equals("ngayHoi")){
			result = findByNgayHoi(keyWord);
		} else if (criteria.equals("ngayTraLoi")){
			result = findByNgayTraLoi(keyWord);
		} else if (criteria.equals("bacSi")){
			result = findByIdNguoiTraLoiLike(keyWord);
		} 
		return result;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<CauHoiTrucTuyen> findAllChuaTraLoi() {
		List<CauHoiTrucTuyen> list = new ArrayList<CauHoiTrucTuyen>();
		Query query = getSession().createQuery("SELECT cauHoi.idCauHoiTrucTuyen, cauHoi.chuDe, cauHoi.tieuDe, cauHoi.nguoiHoi, cauHoi.ngayHoi, cauHoi.traLoi, cauHoi.daXem FROM CauHoiTrucTuyen AS cauHoi WHERE cauHoi.traLoi = null ORDER BY cauHoi.ngayHoi");
		Iterator iter = query.list().iterator();
		CauHoiTrucTuyen cauHoi = null;
		while(iter.hasNext()){
			Object[] row = (Object[])iter.next();
			long idCauHoi = (Long)row[0];
			ChuDe chuDe = (ChuDe)row[1];
			String tieuDe = (String)row[2];
			NguoiDung nguoiHoi = (NguoiDung)row[3];
			Date ngayHoi = (Date)row[4];
			String traLoi = (String)row[5];
			boolean daXem = (Boolean)row[6];
			NguoiDung nguoiHoiSimple = new NguoiDung(nguoiHoi.getIdNguoiDung(), null, null, nguoiHoi.getHoTen(), nguoiHoi.getEmail());
			cauHoi = new CauHoiTrucTuyen(chuDe, tieuDe, null, nguoiHoiSimple, ngayHoi, traLoi, null, null);
			cauHoi.setIdCauHoiTrucTuyen(idCauHoi);
			cauHoi.setDaXem(daXem);
			list.add(cauHoi);
		}
		return list;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<CauHoiTrucTuyen> findAllChuaTraLoiByIdNguoiTraLoi(String idNguoiTraLoi) {
		List<CauHoiTrucTuyen> list = new ArrayList<CauHoiTrucTuyen>();
		Query query = getSession().createQuery("SELECT cauHoi.idCauHoiTrucTuyen, cauHoi.chuDe, cauHoi.tieuDe, cauHoi.nguoiHoi, cauHoi.ngayHoi, cauHoi.traLoi, cauHoi.daXem FROM CauHoiTrucTuyen AS cauHoi WHERE cauHoi.traLoi = null AND cauHoi.nguoiTraLoi.idNguoiDung = :pid ORDER BY cauHoi.ngayHoi")
		.setString("pid", idNguoiTraLoi);
		Iterator iter = query.list().iterator();
		CauHoiTrucTuyen cauHoi = null;
		while(iter.hasNext()){
			Object[] row = (Object[])iter.next();
			long idCauHoi = (Long)row[0];
			ChuDe chuDe = (ChuDe)row[1];
			String tieuDe = (String)row[2];
			NguoiDung nguoiHoi = (NguoiDung)row[3];
			Date ngayHoi = (Date)row[4];
			String traLoi = (String)row[5];
			boolean daXem = (Boolean)row[6];
			NguoiDung nguoiHoiSimple = new NguoiDung(nguoiHoi.getIdNguoiDung(), null, null, nguoiHoi.getHoTen(), nguoiHoi.getEmail());
			cauHoi = new CauHoiTrucTuyen(chuDe, tieuDe, null, nguoiHoiSimple, ngayHoi, traLoi, null, null);
			cauHoi.setIdCauHoiTrucTuyen(idCauHoi);
			cauHoi.setDaXem(daXem);
			list.add(cauHoi);
		}
		return list;
	}
}
